<?php
require_once '../sub/init.php';
//@ini_set('memory_limit', '-1');
$currfields = 'sign_pdf_path,regtime,sign_pdf_flag,RZ';
require_once ZEAI.'m/my_chk_u.php';
require_once ZEAI.'sub/zeai_up_func.php';
require_once ZEAI.'cache/config_vip.php';
require_once ZEAI.'m/sign/chinese.php';
require_once ZEAI.'m/sign/fpdi/fpdi.php';
$cook_md5_regtime = substr(md5($row['regtime']),0,5);
$cook_sign_pdf_path = $row['sign_pdf_path'];
$cook_sign_pdf_flag = $row['sign_pdf_flag'];
$cook_RZ = $row['RZ'];$RZarr=explode(',',$cook_RZ);
$pdf_dst=ZEAI.'up/'.$cook_sign_pdf_path;
$sign=json_decode($_ZEAI['sign'],true);
if($sign['ifidentity']==1 && !in_array('identity',$RZarr))zeaialert('{"title":"应法律法规要求：<br>签名前必须先【实名认证】","btnt_back":"以后再说","btnt_back_url":"","btnt":"立即认证","url":"'.HOST.'/m/my_cert.php?t=identity"}');
if($sign['ifphoto']==1 && !in_array('photo',$RZarr))zeaialert('{"title":"应法律法规要求：<br>签名前必须先【真人认证】","btnt_back":"以后再说","btnt_back_url":"","btnt":"立即认证","url":"'.HOST.'/m/my_cert.php?t=photo"}');
if($submitok=='ajax_save'){
	if(empty($cook_sign_pdf_path))json_exit(array('flag'=>0,'msg'=>'库为空请联系管理员'));
	require_once ZEAI.'m/sign/function.php';
	$img = $_POST['base64'];
	$img = str_replace('data:image/png;64,','',$img);
	$img = str_replace('data:image/png;base64,','',$img);
	$img = str_replace(' ', '+', $img);
	$data = base64_decode($img);
	$png_path_tmp = str_replace(".pdf","_tmp.png",$cook_sign_pdf_path);
	file_put_contents(ZEAI.'up/'.$png_path_tmp,$data);
	zipImg(ZEAI.'up/'.$png_path_tmp,$data);
	//
	$pdf = new FPDI();
	$pdf->AddPage();
	$pdf->setSourceFile('../up/p/img/sign_mb.pdf');
	$tplIdx = $pdf->importPage(1);
	$pdf->useTemplate($tplIdx);
	$pdf->SetTextColor(0,0,0);
	$pdf->SetXY(1,67);
	$pdf -> AddGBFont ('GB',iconv("UTF-8","gbk",'黑体'));  
	$pdf->SetFont('GB','',14); 
	$pdf->SetXY(-110,220);
	$pdf -> Cell(100,18,iconv("UTF-8","gbk",'在本平台ID：'.$cook_uid),0,2,'L');
	$pdf -> Cell(100,18,iconv("UTF-8","gbk",'承诺人签字：'),0,2,'L');
	$pdf -> Cell(100,18,iconv("UTF-8","gbk",'　签字日期：'.date('Y-m-d')),0,2,'L');
	$pdf->Image(ZEAI.'up/'.$png_path_tmp, 130, 238, 50,20 );
	if(!empty($sign['watermark'])){
		$pdf->Image(ZEAI.'up/'.$sign['watermark'], 0, 0, $x,$y);
	}
	$pdf->Output($pdf_dst,'F');
	pdf2png($pdf_dst);
	UP_modFn('u','del',$png_path_tmp);
	$pdf->Close();
	$flag=($sign['flag']==1)?2:1;
	$db->query("UPDATE ".__TBL_USER__." SET sign_pdf_flag=".$flag." WHERE id=".$cook_uid);
	json_exit(array('flag'=>1,'msg'=>'保存成功'));
}?>
<!doctype html><html><head><meta charset="utf-8">
<title>我的-<?php echo $_ZEAI['siteName'];?></title>
<?php echo HEADMETA; ?>
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection" content="telephone=no, email=no" />
<meta name="renderer" content="webkit">
<meta name="x5-fullscreen" content="true">
<meta name="full-screen" content="yes">
<meta name="browsermode" content="application">
<link href="<?php echo RES;?>m/css/m.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
<link href="<?php echo HOST;?>/cache/zeaicolor.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
<link href="<?php echo RES;?>/m/css/my_mateshow.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
<script src="<?php echo RES;?>www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo RES;?>m/js/m.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo HOST;?>/m/sign/vue.js"></script>
<style>
<?php
if ($_ZEAI['m_mb']==2){
	echo '.TRnav_btn i{color:'.$_ZEAI['mob_mbcolor'].'}';
	$mini_class='top_mini4';
}else{
	$mini_class='top_mini4 zeaibg bgcolor yunbox';
}
$mini_title='我的承诺书';
?>
.mask{background-color:rgba(0,0,0,.8)}
.pdfview img{width:100%}
.sign_btm1{width:90%;position:fixed;max-width:750px;height:5rem;left:0;right:0;margin:auto;bottom:4rem;display:-webkit-flex;display:flex;-webkit-flex-wrap:wrap;flex-wrap:wrap;-webkit-justify-content:space-around;justify-content:space-around;padding-bottom:constant(safe-area-inset-bottom);padding-bottom:env(safe-area-inset-bottom);}
.sign_btm1 button{height:5rem;line-height:5rem;font-size:1.6rem;color:#fff;border-radius:3rem;border:0;box-sizing:border-box;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;overflow:hidden;padding-right:1rem}
.sign_btm1 button{width:80%;box-shadow:0 .3rem .8rem rgba(<?php echo c16_rgb($_ZEAI['mob_mbcolor']);?>,.4);background:-webkit-linear-gradient(315deg,rgba(<?php echo c16_rgb($_ZEAI['mob_mbcolor']);?>,.6) 0%,<?php echo $_ZEAI['mob_mbcolor'];?> 100%)}
/****/
#sign_box{display:none}
#canvasBox {font-size:1.2rem;position:relative;background-color:rgba(<?php echo c16_rgb($_ZEAI['mob_mbcolor']);?>,.1);overflow:hidden;border-radius:.8rem;margin:0 auto}
#canvasBox #signcanvas {position:relative;z-index:99;cursor:crosshair;}
#canvasBox .background-txt {position:absolute;top:0;bottom:0;margin:auto;width:100%;height:2.2rem;text-align: center;font-size:2.2rem;color: #ddd;letter-spacing: .2rem;z-index: 66}
.sign_btm{position:absolute;bottom:2rem;left:3%;width:94%;margin:2rem auto 0 auto;display:-webkit-flex;display:flex;-webkit-flex-wrap:wrap;flex-wrap:wrap;-webkit-justify-content:space-around;justify-content:space-around}
.sign_btm button{height:4.8rem;line-height:4.8rem;font-size:1.6rem;color:#fff;border-radius:3rem;border:0;box-sizing:border-box;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;overflow:hidden;padding-right:1rem}
.sign_btm button i{width:3.8rem;height:3.8rem;line-height:3.8rem;border-radius:2rem;background-color:#fff;font-size:2rem;float:left;margin:.5rem 0 0 .6rem}
.sign_btm button:first-child{width:32%;background-color:#fff;box-shadow:0 .3rem .8rem rgba(<?php echo c16_rgb($_ZEAI['mob_mbcolor']);?>,.4);color:#333}
.sign_btm button:first-child i{color:#999;border:#f2f2f2 1px solid;box-sizing:border-box;-webkit-box-sizing:border-box;-moz-box-sizing:border-box}
.sign_btm button:last-child{width:45%;box-shadow:0 .3rem .8rem rgba(<?php echo c16_rgb($_ZEAI['mob_mbcolor']);?>,.4);background:-webkit-linear-gradient(315deg,rgba(<?php echo c16_rgb($_ZEAI['mob_mbcolor']);?>,.6) 0%,<?php echo $_ZEAI['mob_mbcolor'];?> 100%)}
.sign_btm button:last-child i{color:<?php echo $_ZEAI['mob_mbcolor'];?>}
.flag2{width:100%;line-height:3rem;margin:5rem auto 0 auto;background-color:rgba(<?php echo c16_rgb($_ZEAI['mob_mbcolor']);?>,.5);color:#fff}
</style>
</head>
<body>
<?php
require_once ZEAI.'m/top_mini.php';
if(empty($cook_sign_pdf_path)){
	$pdf = new FPDI();
	$pdf->AddPage();
	$pdf->setSourceFile('../up/p/img/sign_mb.pdf');
	$tplIdx = $pdf->importPage(1);
	$pdf->useTemplate($tplIdx);
	$pdf->SetTextColor(0,0,0);
	$pdf->SetXY(1,67);
	$pdf -> AddGBFont ('GB',iconv("UTF-8","gbk",'黑体'));  
	$pdf->SetFont('GB','',14); 
	$x=intval($pdf -> w);$y=intval($pdf -> h);
	$pdf->SetXY(-110,220);
	$pdf -> Cell(100,18,iconv("UTF-8","gbk",'在本平台ID：'.$cook_uid),0,2,'L');
	$pdf -> Cell(100,18,iconv("UTF-8","gbk",'承诺人签字：'),0,2,'L');
	$filename = $cook_uid.'_'.$cook_md5_regtime.'.pdf';
	//
	$dbdir = 'p/pdf/'.date('Y').'/'.date('m').'/';
	$dbname = $dbdir.$filename;
	$filename_save = ZEAI.'up/'.$dbdir.$filename;
	mk_dir('../up/'.$dbdir);
	$db->query("UPDATE ".__TBL_USER__." SET sign_pdf_path='$dbname' WHERE id=".$cook_uid);
	$pdf->Output($filename_save, 'F');
	pdf2png($filename_save);
	$pdf->Close();
	UP_modFn('u','del',$dbname);
	header("Location: my_sign.php");exit;
}else{
	$dst_png = ZEAI.'up/'.str_replace(".pdf",".png",$cook_sign_pdf_path);;
	if(!file_exists($dst_png)){
		$db->query("UPDATE ".__TBL_USER__." SET sign_pdf_path='' WHERE id=".$cook_uid);
		header("Location: my_sign.php");exit;
	}
}
if($cook_sign_pdf_flag==2 && !empty($cook_sign_pdf_path)){
	echo '<div class="flag2">当前状态：审核中</div>';
}
?>
<div class="pdfview">
	<img src="<?php echo UP.str_replace(".pdf",".png",$cook_sign_pdf_path).'?'.ADDTIME;?>" id="sign_btn_png">
</div>
<div class="sign_btm1">
	<?php if(!file_exists($pdf_dst)){?>
    <button id="sign_btn"><i class="ico">&#xe610;</i> 开始签名</button>
    <?php }else{?>
    <script>zeai.msg('可在承诺书上面按住不放保存图片',{time:5});</script>
    <button onClick="zeai.openurl('<?php echo UP.$cook_sign_pdf_path;?>');"><i class="ico">&#xe669;</i> 下载PDF签名文件</button>
    <?php }?>
</div>
<?php if(!file_exists($pdf_dst)){?>
<div id="sign_box">
    <div id="canvasBox" :style="getHorizontalStyle">
      <div class="background-txt" v-show="isempty">请在此区域进行签名</div>
      <canvas ref='signcanvas' id="signcanvas" :width="getHorizontalStyle.width" :height="getHorizontalStyle.height"></canvas>
    </div>
    <div class="sign_btm">
        <button class="default" @touchstart="clear" title="button" @mousedown="clear"><i class="ico">&#xe69c;</i>重写</button>
        <button class="" @touchstart="upload" title="button"><i class="ico">&#xe6b1;</i>确认签署</button>
    </div>  
</div>
<script src="sign/draw.js"></script>
<script>
sign_btn.onclick=singFn;
sign_btn_png.onclick=singFn;
function singFn(){
	ZeaiM.div2({obj:sign_box,title:'请手写【本人真实姓名】',h:36});
	var app = new Vue({
	el: '#sign_box',
	data: {
	  degree:0,//90
	  color:1,
	  penColor: '#000',
	  isempty: true,
	  borderColor: [{color: '#000',tag: 1},{color: '#f00',tag: 0},{color: '#00f',tag: 2}],
	  draw: null
	},
	mounted() {
	  this.initCanvas();
	  document.body.addEventListener('touchmove', (e) => {
		e.preventDefault();
	  }, {
		passive: false
	  });
	},
	destroyed() {
	  document.body.removeEventListener('touchmove', (e) => {
		e.preventDefault();
	  }, {
		passive: false
	  });
	},
	computed: {
	  getHorizontalStyle() {
		let d = document;
		let w = window.innerWidth || d.documentElement.clientWidth || d.body.clientWidth;
		let h = window.innerHeight || d.documentElement.clientHeight || d.body.clientHeight;
		let length = (h - w) / 2;
		let width = w-50;
		let height = h/4;
		switch (this.degree) {
		  case -90:
			length = -length;
		  case 90:
			width = h;
			height = w;
			break;
		  default:
			length = 0;
		}
		if (this.canvasBox) {
		  this.canvasBox.removeChild(document.querySelector('canvas'));
		  this.canvasBox.appendChild(document.createElement('canvas'));
		  setTimeout(() => {
			this.initCanvas();
		  }, 200);
		}
		return {
		  transform: `rotate(${this.degree}deg) translate(${length}px,${length}px)`,
		  width: `${width}px`,
		  height: `${height}px`,
		  transformOrigin: 'center center',
		};
	  },
	},
	methods: {
	  initCanvas() {
		//console.log(Draw)
		this.$nextTick(() => {
		  setTimeout(() => {
			let canvas = this.$refs.signcanvas || document.querySelector('#signcanvas');
			this.draw = new Draw(canvas, -this.degree, this.penColor);
		  }, 500)
		})
	  },
	  clear() {
		this.draw.clear();
	  },
	  download() {
		this.draw.downloadPNGImage(this.draw.getPNGImage());
	  },
	  upload() {
		let image = this.draw.getPNGImage();
		let blob = this.draw.dataURLtoBlob(image);
		if(!this.isCanvasBlank()){
			zeai.msg('正在生成保存...',{time:10});
			zeai.ajax({url:'my_sign'+zeai.ajxext+'submitok=ajax_save',data:{base64:image}},function(e){var rs=zeai.jsoneval(e);
				if(rs.flag==1){
					setTimeout(function(){location.reload(true);},1000);
				}else{
					zeai.msg(0);zeai.msg(rs.msg);
				}
			});
		}else{
			zeai.msg('请手写自己的真实姓名');
			return false;
		}
	  },
	  choiceColor(i, type) {
		this.color = i + '';
		this.penColor = type.color
		this.clear();
		this.initCanvas();
		this.hideTiptext();
	  },
	  //隐藏提示文字
	  hideTiptext() {
		this.isempty = this.isCanvasBlank();
	  },
	  // 检测canvas 非空
	  isCanvasBlank(canvas) {
		let emcan = document.querySelector('#signcanvas');
		var blank = document.createElement('canvas'); //系统获取一个空canvas对象
		blank.width = emcan.width;
		blank.height = emcan.height;
		return emcan.toDataURL() == blank.toDataURL(); //比较值相等则为空
	  },
	  //onItemClick 
	  onItemClick(index) {
		if (index == 1) {
		  this.$router.push('addsignaturemodule')
		}
	  }
	}
	})
}
</script>
<?php }
function pdf2png($PDF){
   if(!extension_loaded('imagick')){return false;}
   if(!file_exists($PDF)){return false;}
   $IM =new imagick();
   $IM->setResolution(120,120);
   $IM->setCompressionQuality(100);
   $IM->readImage($PDF);
   $Filename = str_replace(".pdf",".png",$PDF);
   foreach($IM as $Key => $Var){
       $Var->setImageFormat('png');
       if($Var->writeImage($Filename)==true){
           $Return[]= $Filename;
       }
   }
   return $Return;
}
require_once ZEAI.'m/TRnav.php';?>
</body></html>